﻿// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
//
// SearchResultsPage.xaml.h
// Declaration of the SearchResultsPage class.
//

#pragma once

#include "Common\BooleanToVisibilityConverter.h" // Required by generated header
#include "ILocationService.h"
#include "Common\SuspensionManager.h" //Required for Activation
#include "Common\NavigationHelper.h"
#include "SearchResult.h"
#include "SearchResultsPage.g.h"

namespace Weathr
{
    /// <summary>
    /// This page displays search results when a global search is directed to this application.
    /// </summary>
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class SearchResultsPage sealed
    {
    public:
        SearchResultsPage();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        property Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::Object^>^ DefaultViewModel
        {
            Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::Object^>^  get();
        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        property Common::NavigationHelper^ NavigationHelper
        {
            Common::NavigationHelper^ get();
        }

    protected:
        virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
        virtual void OnNavigatedFrom(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;

    private:
        void LoadState(Platform::Object^ sender, Common::LoadStateEventArgs^ e);
        void Filter_Checked(Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

        void ResultsGridView_ItemClick(Platform::Object^ sender, Windows::UI::Xaml::Controls::ItemClickEventArgs^ e);
        void Image_ImageFailed(Platform::Object^ sender, Windows::UI::Xaml::ExceptionRoutedEventArgs^ e);

        concurrency::task<void> GetFlagImageAsync(const std::vector<SearchResult^>& searchResults, Platform::String^ fileName);

        static Windows::UI::Xaml::DependencyProperty^ _defaultViewModelProperty;
        static Windows::UI::Xaml::DependencyProperty^ _navigationHelperProperty;
        std::shared_ptr<ExceptionPolicy> m_exceptionPolicy;

        concurrency::cancellation_token_source m_searchCancellationToken;
        Platform::String^ m_defaultImagePath;

        // Retrieves results from the Bing Maps location service.
        concurrency::task<std::vector<std::shared_ptr<Web::Location>>> GetResultsAsync(Platform::String^ queryText);

        void ShowResults(const std::vector<std::shared_ptr<Web::Location>> &results);

        // Downloads a flag image and caches it for later use.
        concurrency::task<bool> DownloadFlagImageAsync(Windows::Storage::StorageFolder^ folder, Platform::String^ fileName);
    };

    /// <summary>
    /// View model describing one of the filters available for viewing search results.
    /// </summary>
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class SearchResultsPageFilter sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    private:
        Platform::String^ _name;
        int _count;
        bool _active;

    public:
        SearchResultsPageFilter(Platform::String^ name, int count, bool active);

        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        property Platform::String^ Name
        {
            Platform::String^ get();
            void set(Platform::String^ value);
        };

        property int Count
        {
            int get();
            void set(int value);
        };

        property bool Active
        {
            bool get();
            void set(bool value);
        };

        property Platform::String^ Description
        {
            Platform::String^ get();
        };

    protected:
        virtual void OnPropertyChanged(Platform::String^ propertyName);
    };
}
